<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-10-18
 * Time: 11:06
 */

namespace app\manage\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

// 引入ZBuilder
use app\common\model\TaskType as TaskTypeModel;
use think\Db;

class Task extends Admin
{
    //新闻分类列表
    public function lists( )
    {
        // 读取数据
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('qt_task')
            ->alias('t')
            ->leftJoin('qt_task_type tp','t.task_id = tp.id')
            ->field('t.*,tp.name')
            ->where($map)
            ->order('id desc')
            ->paginate()->each(function ($item){
                $item['process_name'] = Db::name('qt_users')->where('id',$item['pro_user_id'])->value('nickname');
                $item['receive_name'] = Db::name('qt_users')->where('id',$item['receive_user_id'])->value('nickname');
                return $item;
            });
        // 分页数据
        $page = $data_list->render();
        $btn_access1 = [
            'title' => '查看任务',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('look', ['id' => '__id__']),
        ];
        $task_type = Db::name('qt_task_type')->order('id asc')->column('id,name');
        $postRes = Db::name('qt_post')->where('status','1')->order('id asc')->column('id,title');
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('user_name', '派发人', 'text')
            ->addColumn('process_name', '审批人', 'text')
            ->addColumn('receive_name', '接收人', 'text')
            ->addColumn('task_time', '派发时间', 'datetime', '', 'Y-m-d H:i:s')
            ->addColumn('num_type', '分数类型', 'status','',['1' => 'A分', '2' => 'B分'])
            ->addColumn('num', '分数值', 'text')
            ->addColumn('task_id', '任务分类', 'status','',$task_type)
            ->addColumn('time_type', '时间要求', 'status','',['1' => '按时', '2' => '加急','3'=>'可以超时'])
            ->addColumn('right_button', '操作', 'btn')
            ->setSearchArea([
                ['text:2', 't.user_name', '发布人'],
            ])
            ->addRightButton('look', $btn_access1) // 添加右侧按钮
            ->setPrimaryKey('id') // 设置主键名为username
            ->setColumnWidth([
                'id'  => 30,
                'status' => 40,
                'right_button'=>50,
                'name'=>100,
                'title'=>60
            ])
            ->setPageTitle('任务列表')
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch();
    }

    //添加日报审批分类
    public function add( )
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'DailyType.add');
            if (true !== $result) $this->error($result);
            $data['created_time'] = time();
            $data['pid'] = '0';
            if (TaskTypeModel::create($data)) {
                $this->success('新增成功', url('lists'));
            } else {
                $this->error('新增失败');
            }
        } else {
            $post_category = Db::name('qt_post')->order('id asc')->column('id,title');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'name', '分类名称'],
                        ['text:7', 'sort', '排序','','50'],
                        ['select:7', 'post_id', '选择岗位', '<code>请选择所属岗位</code>', $post_category],
                        ['radio:', 'status', '状态', '', ['1' => '显示', '2' => '隐藏'], '1'],
                    ]
                )
                ->setPageTitle('新增一级分类')
                ->fetch();
        }
    }


    //编辑任务分类
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'DailyType.add');
            if (true !== $result) $this->error($result);

            if (TaskTypeModel::update($data)) {
                $this->success('编辑成功', 'lists');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = TaskTypeModel::get($id);
            $post_category = Db::name('qt_post')->order('id asc')->column('id,title');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden', 'id', 'id'],
                        ['text:7', 'name', '标题'],
                        ['text:7', 'sort', '排序','','50'],
                        ['select:7', 'post_id', '选择岗位', '<code>请选择所属岗位</code>', $post_category],
                        ['radio', 'status', '状态', '', [1 => '显示', 2 => '隐藏']]
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑分类')
                ->fetch();
        }
    }




    //编辑任务分类
    public function look($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'DailyType.add');
            if (true !== $result) $this->error($result);

            if (TaskTypeModel::update($data)) {
                $this->success('编辑成功', 'lists');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = Db::name('qt_task')->where('id',$id)->find();
            $info['pro_user'] = Db::name('qt_users')->where('id',$info['pro_user_id'])->value('nickname');
            $info['receive_user'] = Db::name('qt_users')->where('id',$info['receive_user_id'])->value('nickname');
            $info['cc_user'] = Db::name('qt_users')->where('id',$info['cc_user_id'])->value('nickname');
            $info['task_type'] = Db::name('qt_task_type')->where('id',$info['task_id'])->value('name');

            $post_category = Db::name('qt_post')->order('id asc')->column('id,title');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden', 'id', 'id'],
                        ['text:7', 'user_name', '派发人'],
                        ['text:7', 'pro_user', '审批人'],
                        ['text:7', 'receive_user', '接收人'],
                        ['text:7', 'cc_user', '抄送人'],
                        ['radio', 'num_type', '分数类型', '', [1 => 'A分', 2 => 'B分']],
                        ['text:7', 'num', '分数值'],
                        ['text:7', 'task_type', '任务类型'],
                        ['radio', 'time_type', '时间要求', '', [1 => '按时', 2 => '加急','3'=>'可以超时']],
                        ['datetime:7', 'complete_time', '完成时间'],
                        ['text:7', 'complete_num', '完成分数'],
                        ['image:7', 'image', '图片'],
                        ['text:7', 'content', '任务说明'],
                        ['radio', 'status', '状态', '', [1 => '新派发', 2 => '待办中','3'=>'已完成']],
                        ['datetime:7', 'task_time', '派发时间'],
                        ['datetime:7', 'ent_time', '完成时间'],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('查看任务')
                ->fetch();
        }
    }











    //删除任务分类
    public function delete($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        $son = Db::name('qt_task')->where('task_type', $id)->count();
        if ($son > 0) {
            $this->error('请先删除下属分类', 'lists');
        }
        $re = TaskTypeModel::destroy($id);// 删除主键为1,2,3的数据
        if ($re) {
            $this->success('删除成功', 'lists');
        } else {
            $this->error('系统繁忙请稍后再试', 'lists');
        }

    }


}